package cn.wolfcode.car.business.service.impl;


import cn.wolfcode.car.business.domain.Statement;
import cn.wolfcode.car.business.domain.StatementItem;
import cn.wolfcode.car.business.mapper.StatementItemMapper;
import cn.wolfcode.car.business.query.StatementItemQuery;
import cn.wolfcode.car.business.service.IAppointmentService;
import cn.wolfcode.car.business.service.IStatementItemService;
import cn.wolfcode.car.business.service.IStatementService;
import cn.wolfcode.car.common.base.page.TablePageInfo;
import cn.wolfcode.car.common.exception.BusinessException;
import cn.wolfcode.car.common.util.Convert;
import com.github.pagehelper.PageHelper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.math.BigDecimal;
import java.util.Date;
import java.util.List;
import java.util.stream.Collectors;

@Service
@Transactional
public class StatementItemServiceImpl implements IStatementItemService {

    @Autowired
    private StatementItemMapper statementItemMapper;

    @Autowired
    private IAppointmentService appointmentService;

    @Autowired
    private IStatementService statementService;


    @Override
    public TablePageInfo<StatementItem> query(StatementItemQuery qo) {
        PageHelper.startPage(qo.getPageNum(), qo.getPageSize());
        return new TablePageInfo<StatementItem>(statementItemMapper.selectForList(qo));
    }


    @Override
    public void save(StatementItem statementItem) {
        statementItemMapper.insert(statementItem);
    }

    @Override
    public StatementItem get(Long id) {
        return statementItemMapper.selectByPrimaryKey(id);
    }


    @Override
    public void update(StatementItem statementItem) {
        statementItemMapper.updateByPrimaryKey(statementItem);
    }

    @Override
    public void deleteBatch(String ids) {
        Long[] dictIds = Convert.toLongArray(ids);
        for (Long dictId : dictIds) {
            statementItemMapper.deleteByPrimaryKey(dictId);
        }
    }

    @Override
    public List<StatementItem> list() {
        return statementItemMapper.selectAll();
    }

    @Override
    public void saveItems(List<StatementItem> statementItems) {

      if(statementItems==null||statementItems.size()<1){
          throw new BusinessException("传入参数有误");
      }
      StatementItem last = statementItems.remove(statementItems.size() - 1);

        Statement temp = statementService.get(last.getStatementId());

        if(Statement.STATUS_PAID.equals(temp.getStatus())){
            throw new BusinessException("已经支付订单无法修改");
        }

        statementItemMapper.deleteByStatementId(last.getStatementId());

        BigDecimal totalAmount = new BigDecimal("0.00");
        BigDecimal discountAmount = last.getItemPrice();
        BigDecimal totalQuantity = new BigDecimal("0");

        if(statementItems.size()==0){
            statementService.updateByIdAndAmount(last.getStatementId(),totalAmount,discountAmount,totalQuantity);
            return;
        }

        for (StatementItem statementItem : statementItems) {
            totalAmount=totalAmount.add(statementItem.getItemPrice().multiply(new BigDecimal(statementItem.getItemQuantity())));
            totalQuantity=totalQuantity.add(new BigDecimal(statementItem.getItemQuantity()));
            statementItemMapper.insert(statementItem);
        }
        if(totalAmount.compareTo(last.getItemPrice())<0){

            throw new BusinessException("折扣金额有误");
        }


        statementService.updateByIdAndAmount(last.getStatementId(),totalAmount,discountAmount,totalQuantity);



    }

    @Override
    public void payStatement(Long statementId) {
        if(statementId==null){
            throw new BusinessException("传入参数有误");
        }
        Statement statement = statementService.get(statementId);
        if(statement==null){
            throw new BusinessException("该订单不存在");
        }
        if(Statement.STATUS_PAID.equals(statement.getStatus())){
           throw new BusinessException("该订单已经支付无法重新支付") ;
        }
        if(statement.getAppointmentId()!=null){
            appointmentService.payStatement(statement.getAppointmentId());
        }
        statementService.payStatement(statementId);

    }

    @Override
    public void deleteByStatmentId(Long id) {
        statementItemMapper.deleteByStatementId(id);
    }


}
